<?php
/**
 * 点大商城（www.diandashop.com） - 微信公众号小程序商城系统!
 * Copyright © 2020 山东点大网络科技有限公司 保留所有权利
 * =========================================================
 * 版本：V2
 * 授权主体：张家口微智网络科技有限公司
 * 授权域名：ddv2.zjkwzwl.com
 * 授权码：iTpYWSgMSYeQCYPrTD
 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

namespace app\controller;
use think\facade\Db;
class ApiWxpay extends ApiCommon
{
	public function initialize(){
		parent::initialize();
		$this->checklogin();
		if(platform=='mp' && !$this->member['mpopenid']){
			$appinfo = \app\common\System::appinfo(aid,'mp');
			$appid = $appinfo['appid'];
			$appsecret = $appinfo['appsecret'];
			$authtype = $appinfo['authtype'];
			
			if($authtype==1){ //授权接入
				$component_appid = \app\common\Wechat::component_appid();
			}
			//授权登录
			if(input('param.state') && input('param.state') == 'getMpOpenid' && input('param.code')){
				$code = input('param.code');
				if($authtype==1){ //授权接入
					$url = "https://api.weixin.qq.com/sns/oauth2/component/access_token?appid={$appid}&code={$code}&grant_type=authorization_code&component_appid={$component_appid}&component_access_token=".\app\common\Wechat::component_access_token(aid);
				}else{
					$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$appsecret}&code={$code}&grant_type=authorization_code";
				}
				$result = request_get($url);
				$rs = json_decode($result);
				if($rs->openid){
					Db::name('member')->where('id',mid)->update(['mpopenid'=>$rs->openid]);
				}
				header('location:'.input('param.thisurl'));
			}else{
				
				//获取用户openid
				$request_url = ltrim($_SERVER["REQUEST_URI"],'/');
				if(strpos($request_url,'?code=')!==false){
					$request_url = explode('?code=',$request_url)[0];
				}elseif(strpos($request_url,'&code=')!==false){
					$request_url = explode('&code=',$request_url)[0];
				}
				$redirectUrl = request()->domain().'/'.$request_url.'&thisurl='.urlencode(input('param.thisurl'));
				$redirectUrl = urlencode($redirectUrl);

				if($authtype==1){ //授权接入
					$getOpenidUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&component_appid={$component_appid}&redirect_uri={$redirectUrl}&response_type=code&scope=snsapi_base&state=getMpOpenid#wechat_redirect";
				}else{
					$getOpenidUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$redirectUrl}&response_type=code&scope=snsapi_base&state=getMpOpenid#wechat_redirect";
				}
				die(jsonEncode(['status'=>-2,'msg'=>'获取用户openid','url'=>$getOpenidUrl]));
			}
		}
	}
	//余额充值
	public function recharge(){
		$canrecharge = Db::name('admin_set')->where('aid',aid)->value('recharge');
		$giveset = Db::name('recharge_giveset')->where('aid',aid)->find();
		if($giveset && $giveset['status']==1){
			$givedata = json_decode($giveset['givedata'],true);
		}else{
			$givedata = [];
		}
		if(request()->isPost()){
			if($canrecharge == 0) return $this->json(['status'=>0,'msg'=>t('余额').'充值功能未启用']);
			$money = input('post.money');
			if($money>0){
				$ordernum = date('ymdHis').aid.rand(1000,9999);
				//增加消费记录
				$record['aid'] = aid;
				$record['mid'] = mid;
				$record['createtime']= time();
				$record['money'] = $money;
				$record['ordernum'] = $ordernum;
				$recordid = Db::name('recharge_order')->insertGetId($record);
				if(input('post.paytype') == 'alipay'){
					$rs = \app\common\Alipay::build_h5(aid,mid,'充值'.t('余额'),$ordernum,$money,'recharge_order','',PRE_URL.'/m.php?s=/my/usercenter/aid/'.aid);
					return $this->json($rs);
				}else{
					if(platform == 'tt' || platform == 'h5'){
						$rs = \app\common\Wxpay::build_h5(aid,mid,'充值'.t('余额'),$ordernum,$money,'recharge_order');
					}else{
						$rs = \app\common\Wxpay::build(aid,mid,'充值'.t('余额'),$ordernum,$money,'recharge_order');
					}
					$rs['give_coupon_list'] = \app\common\Coupon::getpaygive(aid,mid,'recharge',$money);
					return $this->json($rs);
				}
			}else{
				return $this->json(['status'=>0,'msg'=>'充值金额必须大于0']);
			}
		}
		$userinfo = [];
		$userinfo['money'] = $this->member['money'];
		$set = Db::name('admin_set')->where('aid',aid)->find();
		$rdata = [];
		$rdata['canrecharge'] = $canrecharge;
		$rdata['giveset'] = $givedata;
		$rdata['shuoming'] = $giveset['shuoming'];
		$rdata['userinfo'] = $userinfo;
		if(platform == 'wx' || platform == 'mp'){
			$rdata['alipay'] = 0;
		}else{
			$rdata['alipay'] = $set['alipay'];
		}
		$rdata['wxpay'] = $set['wxpay'];
		return $this->json($rdata);
	}
	//商城订单支付
	public function shoppay(){
		$orderid = input('param.orderid/d');
		$order = Db::name('shop_order')->where('id',$orderid)->where('aid',aid)->where('mid',mid)->find();
		if(!$order){
			return $this->json(['status'=>0,'msg'=>'该订单不存在']);
		}
		if($order['status']){
			if($order['status']==4){
				return $this->json(['status'=>0,'msg'=>'该订单已关闭']);
			}else{
				return $this->json(['status'=>0,'msg'=>'该订单已支付']);
			}
		}
		//判断配送时间选择是否符合要求
		if($order['freightid'] && $order['freight_time']){
			$freight = Db::name('freight')->where('id',$order['freightid'])->find();
			if($freight){
				//$freighttime = strtotime(explode('~',$order['freight_time'])[0]);
				$freight_times = explode('~',$order['freight_time']);
				if($freight_times[1]){
					$freighttime = strtotime(explode(' ',$freight_times[0])[0] . ' '.$freight_times[1]);
				}else{
					$freighttime = strtotime($freight_times[0]);
				}
				if(time() + $freight['psprehour']*3600 > $freighttime){
					return $this->json(['status'=>0,'msg'=>($freight['pstype']!=1?'配送':'取货').'时间必须在'.$freight['psprehour'].'小时之后']);
				}
			}
		}
		if(input('param.op') == 'submit'){
			$post = input('post.');
			if($this->member['paypwd'] && $post['typeid']==2){ //余额支付 验证支付密码
				if($post['paypwd'] != $this->member['paypwd']){
					return $this->json(['status'=>0,'msg'=>'支付密码输入错误']);
				}
			}
			if($order['totalprice'] <=0){
				Db::name('shop_order')->where('id',$order['id'])->update(['status'=>1,'paytime'=>time(),'paytype'=>'无须付款']);
				Db::name('shop_order_goods')->where('orderid',$order['id'])->update(['status'=>1]);

				//服务型商品
				if($order['isfuwu']==1){
					$og = Db::name('shop_order_goods')->where('aid',aid)->where('orderid',$order['id'])->find();
					$product = Db::name('shop_product')->where('aid',aid)->where('id',$og['proid'])->find();
					$fuwuendtime = $order['fuwuendtime'] > time() ? $order['fuwuendtime'] + $product['fuwuday']*86400 : time() + $product['fuwuday']*86400;
					Db::name('shop_order')->where('id',$order['id'])->update(['fuwuendtime'=>$fuwuendtime]);
				}
				//自动发货
				if($order['freight_type']==3){
					\app\common\Common::autofh('shop',$order);
				}
				
				//支付后送券
				$couponlist = \app\common\Coupon::getpaygive(aid,mid,'shop',$order['totalprice'],$order['id']);
				if($couponlist){
					foreach($couponlist as $coupon){
						\app\common\Coupon::send(aid,mid,$coupon['id']);
					}
				}

				\app\common\Member::uplv(aid,mid);
				\app\common\Wifiprint::print(aid,'shop',$order['id']);

				//公众号通知 订单支付成功
				$tmplcontent = [];
				$tmplcontent['first'] = '有新订单支付成功';
				$tmplcontent['remark'] = '点击进入查看~';
				$tmplcontent['keyword1'] = $this->member['nickname']; //用户名
				$tmplcontent['keyword2'] = $order['ordernum'];//订单号
				$tmplcontent['keyword3'] = $order['totalprice'].'元';//订单金额
				$tmplcontent['keyword4'] = $order['title'];//商品信息
				\app\common\Wechat::sendhttmpl(aid,$order['bid'],'tmpl_orderpay',$tmplcontent,m_url('admin/order/shoporder'),$order['mdid']);
				$tmplcontent['first'] = '恭喜您的订单已支付成功';
				$rs = \app\common\Wechat::sendtmpl(aid,mid,'tmpl_orderpay',$tmplcontent,m_url('pages/my/usercenter'));
				//短信通知
				if($this->member['tel']){
					$tel = $this->member['tel'];
				}else{
					$tel = $order['tel'];
				}
				$rs = \app\common\Sms::send(aid,$tel,'tmpl_orderpay');

				return $this->json(['status'=>2,'msg'=>'付款成功']);
			}
			if($post['typeid']==2){ //余额支付
				if($this->member['money'] < $order['totalprice']){
					return $this->json(['status'=>0,'msg'=>t('余额').'不足,请充值']);	
				}
				Db::name('shop_order')->where('id',$order['id'])->update(['status'=>1,'paytime'=>time(),'paytype'=>t('余额').'支付']);
				Db::name('shop_order_goods')->where('orderid',$order['id'])->update(['status'=>1]);

				//自动发货
				if($order['freight_type']==3){
					\app\common\Common::autofh('shop',$order);
				}

				//减去会员的余额
				\app\common\Member::addmoney(aid,mid,-$order['totalprice'],'购买商品,订单号: '.$order['ordernum']);
				
				//支付后送券
				$couponlist = \app\common\Coupon::getpaygive(aid,mid,'shop',$order['totalprice'],$order['id']);
				if($couponlist){
					foreach($couponlist as $coupon){
						\app\common\Coupon::send(aid,mid,$coupon['id']);
					}
				}

				\app\common\Member::uplv(aid,mid);
				\app\common\Wifiprint::print(aid,'shop',$order['id']);

				//公众号通知 订单支付成功
				$tmplcontent = [];
				$tmplcontent['first'] = '有新订单支付成功';
				$tmplcontent['remark'] = '点击进入查看~';
				$tmplcontent['keyword1'] = $this->member['nickname']; //用户名
				$tmplcontent['keyword2'] = $order['ordernum'];//订单号
				$tmplcontent['keyword3'] = $order['totalprice'].'元';//订单金额
				$tmplcontent['keyword4'] = $order['title'];//商品信息
				\app\common\Wechat::sendhttmpl(aid,$order['bid'],'tmpl_orderpay',$tmplcontent,m_url('admin/order/shoporder'),$order['mdid']);
				$tmplcontent['first'] = '恭喜您的订单已支付成功';
				$rs = \app\common\Wechat::sendtmpl(aid,mid,'tmpl_orderpay',$tmplcontent,m_url('pages/my/usercenter'));
				//短信通知
				$rs = \app\common\Sms::send(aid,$this->member['tel']?$this->member['tel']:$order['tel'],'tmpl_orderpay');

				return $this->json(['status'=>2,'msg'=>'付款成功']);
			}elseif($post['typeid']==3){//货到付款
				$cancod = Db::name('shop_sysset')->where('aid',aid)->value('cancod');
				if(!$cancod) return $this->json(['status'=>0,'msg'=>'不支持货到付款方式']);
				Db::name('shop_order')->where('id',$order['id'])->update(['status'=>1,'paytime'=>time(),'paytype'=>'货到付款']);
				Db::name('shop_order_goods')->where('orderid',$order['id'])->update(['status'=>1]);

				//服务型商品
				if($order['isfuwu']==1){
					$og = Db::name('shop_order_goods')->where('aid',aid)->where('orderid',$order['id'])->find();
					$product = Db::name('shop_product')->where('aid',aid)->where('id',$og['proid'])->find();
					$fuwuendtime = $order['fuwuendtime'] > time() ? $order['fuwuendtime'] + $product['fuwuday']*86400 : time() + $product['fuwuday']*86400;
					Db::name('shop_order')->where('id',$order['id'])->update(['fuwuendtime'=>$fuwuendtime]);
				}
				//自动发货
				if($order['freight_type']==3){
					\app\common\Common::autofh('shop',$order);
				}

				\app\common\Wifiprint::print(aid,'shop',$order['id']);
				
				//公众号通知 订单支付成功
				$tmplcontent = [];
				$tmplcontent['first'] = '有新订单选择货到付款下单成功';
				$tmplcontent['remark'] = '点击进入查看~';
				$tmplcontent['keyword1'] = $this->member['nickname']; //用户名
				$tmplcontent['keyword2'] = $order['ordernum'];//订单号
				$tmplcontent['keyword3'] = $order['totalprice'].'元';//订单金额
				$tmplcontent['keyword4'] = $order['title'];//商品信息
				\app\common\Wechat::sendhttmpl(aid,$order['bid'],'tmpl_orderpay',$tmplcontent,m_url('pages/order/shoporder'),$order['mdid']);
				$tmplcontent['first'] = '恭喜您的订单已下单成功，支付方式为货到付款';
				$rs = \app\common\Wechat::sendtmpl(aid,mid,'tmpl_orderpay',$tmplcontent,m_url('pages/my/usercenter'));
				return $this->json(['status'=>2,'msg'=>'提交成功']);
			}elseif($post['typeid']==10){ //支付宝支付
				if(platform == 'alipay'){ //支付宝小程序
					$rs = \app\common\Alipay::build(aid,mid,'购买商品',$order['ordernum'],$order['totalprice'],'shop_order');
					return $this->json($rs);
				}else{
					$rs = \app\common\Alipay::build_h5(aid,mid,'购买商品',$order['ordernum'],$order['totalprice'],'shop_order','',m_url('pages/my/usercenter'));
					return $this->json($rs);
				}
			}elseif($post['typeid']==11){
				$rs = \app\common\Baidupay::build(aid,mid,'购买商品',$order['ordernum'],$order['totalprice'],'shop_order');
				return $this->json($rs);
			}else{ //微信支付
				if($order['isfuwu'] == 1){
					$ordernum = date('ymdHis').rand(100000,999999);
					Db::name('shop_order')->where('id',$order['id'])->update(['ordernum'=>$ordernum]);
					$order['ordernum'] = $ordernum;
				}
				if(platform == 'toutiao' || platform == 'h5'){
					$rs = \app\common\Wxpay::build_h5(aid,mid,'购买商品',$order['ordernum'],$order['totalprice'],'shop_order');
				}else{
					$rs = \app\common\Wxpay::build(aid,mid,'购买商品',$order['ordernum'],$order['totalprice'],'shop_order');
				}
				return $this->json($rs);
			}
		}
		$userinfo = [];
		$userinfo['money'] = $this->member['money'];
		if($this->member['paypwd']==''){
			$userinfo['haspwd'] = 0;
		}else{
			$userinfo['haspwd'] = 1;
		}
		$set = Db::name('admin_set')->where('aid',aid)->find();
		$shopset = Db::name('shop_sysset')->where('aid',aid)->find(); 
		//订阅消息
		$wx_tmplset = Db::name('wx_tmplset')->where('aid',aid)->find();
		$tmplids = [];
		if($wx_tmplset['tmpl_orderfahuo']){
			$tmplids[] = $wx_tmplset['tmpl_orderfahuo'];
		}
		if($order['isfuwu'] == 1 && $wx_tmplset['tmpl_fuwudaoqi']){
			$tmplids[] = $wx_tmplset['tmpl_fuwudaoqi'];
		}
		
		$storeinfo = [];
		if($order['freight_type'] == 1){
			$storeinfo = Db::name('mendian')->where('id',$order['mdid'])->field('name,address,longitude,latitude')->find();
		}

		$rdata = [];
		$rdata['orderinfo'] = $order;
		$rdata['storeinfo'] = $storeinfo;
		$rdata['wxpayst'] = $set['wxpay'];
		$rdata['alipay'] = $set['alipay'];
		$rdata['moneypay'] = $set['moneypay'];
		$rdata['userinfo'] = $userinfo;
		$rdata['cancod'] = $shopset['cancod'];//货到付款
		$rdata['codtxt'] = $shopset['codtxt'];
		$rdata['give_coupon_list'] = \app\common\Coupon::getpaygive(aid,mid,'shop',$order['totalprice'],$order['id']);
		$rdata['tmplids'] = $tmplids;
		return $this->json($rdata);
	}
	//商城订单支付
	public function shoppay2(){
		$ordernum = input('param.ordernum');
		$orderlist = Db::name('shop_order')->where('ordernum','like',$ordernum.'%')->where('aid',aid)->where('mid',mid)->select()->toArray();
		if(!$orderlist){
			return $this->json(['status'=>0,'msg'=>'该订单不存在']);
		}
		$totalprice = 0;
		foreach($orderlist as $order){
			if($order['status']){
				if($order['status']==4){
					return $this->json(['status'=>0,'msg'=>'该订单已关闭']);
				}else{
					return $this->json(['status'=>0,'msg'=>'该订单已支付']);
				}
			}
			//判断配送时间选择是否符合要求
			if($order['freightid'] && $order['freight_time']){
				$freight = Db::name('freight')->where('id',$order['freightid'])->find();
				if($freight){
					//$freighttime = strtotime(explode('~',$order['freight_time'])[0]);
					$freight_times = explode('~',$order['freight_time']);
					if($freight_times[1]){
						$freighttime = strtotime(explode(' ',$freight_times[0])[0] . ' '.$freight_times[1]);
					}else{
						$freighttime = strtotime($freight_times[0]);
					}
					if(time() + $freight['psprehour']*3600 > $freighttime){
						return $this->json(['status'=>0,'msg'=>($freight['pstype']!=1?'配送':'取货').'时间必须在'.$freight['psprehour'].'小时之后']);
					}
				}
			}
			$totalprice+=$order['totalprice'];
		}
		
		if(request()->isPost()){
			$post = input('post.');
			if($this->member['paypwd'] && $post['typeid']==2){ //余额支付 验证支付密码
				if($post['paypwd'] != $this->member['paypwd']){
					return $this->json(['status'=>0,'msg'=>'支付密码输入错误']);
				}
			}
			if($totalprice <=0){
				Db::name('shop_order')->where('ordernum','like',$ordernum.'%')->where('aid',aid)->where('mid',mid)->update(['status'=>1,'paytime'=>time(),'paytype'=>'无须付款']);
				Db::name('shop_order_goods')->where('ordernum','like',$ordernum.'%')->where('aid',aid)->where('mid',mid)->update(['status'=>1]);
				
				//支付后送券
				$orderids = Db::name('shop_order')->where('ordernum','like',$ordernum.'%')->where('aid',aid)->where('mid',mid)->column('id');
				$couponlist = \app\common\Coupon::getpaygive(aid,mid,'shop',$totalprice,$orderids);
				if($couponlist){
					foreach($couponlist as $coupon){
						\app\common\Coupon::send(aid,mid,$coupon['id']);
					}
				}
				\app\common\Member::uplv(aid,mid);
				foreach($orderlist as $order){
					
					//服务型商品
					if($order['isfuwu']==1){
						$og = Db::name('shop_order_goods')->where('aid',aid)->where('orderid',$order['id'])->find();
						$product = Db::name('shop_product')->where('aid',aid)->where('id',$og['proid'])->find();
						$fuwuendtime = $order['fuwuendtime'] > time() ? $order['fuwuendtime'] + $product['fuwuday']*86400 : time() + $product['fuwuday']*86400;
						Db::name('shop_order')->where('id',$order['id'])->update(['fuwuendtime'=>$fuwuendtime]);
					}
					//自动发货
					if($order['freight_type']==3){
						\app\common\Common::autofh('shop',$order);
					}

					\app\common\Wifiprint::print(aid,'shop',$order['id']);
					//公众号通知 订单支付成功
					$tmplcontent = [];
					$tmplcontent['first'] = '有新订单支付成功';
					$tmplcontent['remark'] = '点击进入查看~';
					$tmplcontent['keyword1'] = $this->member['nickname']; //用户名
					$tmplcontent['keyword2'] = $order['ordernum'];//订单号
					$tmplcontent['keyword3'] = $order['totalprice'].'元';//订单金额
					$tmplcontent['keyword4'] = $order['title'];//商品信息
					\app\common\Wechat::sendhttmpl(aid,$order['bid'],'tmpl_orderpay',$tmplcontent,m_url('admin/order/shoporder'),$order['mdid']);
					$tmplcontent['first'] = '恭喜您的订单已支付成功';
					$rs = \app\common\Wechat::sendtmpl(aid,mid,'tmpl_orderpay',$tmplcontent,m_url('pages/my/usercenter'));
				}
				return $this->json(['status'=>2,'msg'=>'付款成功']);
			}
			if($post['typeid']==2){ //余额支付
				if($this->member['money'] < $totalprice){
					return $this->json(['status'=>0,'msg'=>t('余额').'不足,请充值']);	
				}
				Db::name('shop_order')->where('ordernum','like',$ordernum.'%')->where('aid',aid)->where('mid',mid)->update(['status'=>1,'paytime'=>time(),'paytype'=>t('余额').'支付']);
				Db::name('shop_order_goods')->where('ordernum','like',$ordernum.'%')->where('aid',aid)->where('mid',mid)->update(['status'=>1]);

				//减去会员的余额
				\app\common\Member::addmoney(aid,mid,-$totalprice,'购买商品,订单号: '.$ordernum);
				
				//支付后送券
				$orderids = Db::name('shop_order')->where('ordernum','like',$ordernum.'%')->where('aid',aid)->where('mid',mid)->column('id');
				$couponlist = \app\common\Coupon::getpaygive(aid,mid,'shop',$totalprice,$orderids);
				if($couponlist){
					foreach($couponlist as $coupon){
						\app\common\Coupon::send(aid,mid,$coupon['id']);
					}
				}

				\app\common\Member::uplv(aid,mid);
				foreach($orderlist as $order){
					
					//服务型商品
					if($order['isfuwu']==1){
						$og = Db::name('shop_order_goods')->where('aid',aid)->where('orderid',$order['id'])->find();
						$product = Db::name('shop_product')->where('aid',aid)->where('id',$og['proid'])->find();
						$fuwuendtime = $order['fuwuendtime'] > time() ? $order['fuwuendtime'] + $product['fuwuday']*86400 : time() + $product['fuwuday']*86400;
						Db::name('shop_order')->where('id',$order['id'])->update(['fuwuendtime'=>$fuwuendtime]);
					}
					//自动发货
					if($order['freight_type']==3){
						\app\common\Common::autofh('shop',$order);
					}

					\app\common\Wifiprint::print(aid,'shop',$order['id']);
					//公众号通知 订单支付成功
					$tmplcontent = [];
					$tmplcontent['first'] = '有新订单支付成功';
					$tmplcontent['remark'] = '点击进入查看~';
					$tmplcontent['keyword1'] = $this->member['nickname']; //用户名
					$tmplcontent['keyword2'] = $order['ordernum'];//订单号
					$tmplcontent['keyword3'] = $order['totalprice'].'元';//订单金额
					$tmplcontent['keyword4'] = $order['title'];//商品信息
					\app\common\Wechat::sendhttmpl(aid,$order['bid'],'tmpl_orderpay',$tmplcontent,m_url('admin/order/shoporder'),$order['mdid']);
					$tmplcontent['first'] = '恭喜您的订单已支付成功';
					$rs = \app\common\Wechat::sendtmpl(aid,mid,'tmpl_orderpay',$tmplcontent,m_url('pages/my/usercenter'));
				}
				//短信通知
				if($this->member['tel']){
					$tel = $this->member['tel'];
				}else{
					$tel = $order['tel'];
				}
				$rs = \app\common\Sms::send(aid,$tel,'tmpl_orderpay');

				return $this->json(['status'=>2,'msg'=>'付款成功']);
			}elseif($post['typeid']==3){//货到付款
				$cancod = Db::name('shop_sysset')->where('aid',aid)->value('cancod');
				if(!$cancod) return $this->json(['status'=>0,'msg'=>'不支持货到付款方式']);
				Db::name('shop_order')->where('ordernum','like',$ordernum.'%')->where('aid',aid)->where('mid',mid)->update(['status'=>1,'paytime'=>time(),'paytype'=>'货到付款']);
				Db::name('shop_order_goods')->where('ordernum','like',$ordernum.'%')->where('aid',aid)->where('mid',mid)->update(['status'=>1]);

				foreach($orderlist as $order){
					//服务型商品
					if($order['isfuwu']==1){
						$og = Db::name('shop_order_goods')->where('aid',aid)->where('orderid',$order['id'])->find();
						$product = Db::name('shop_product')->where('aid',aid)->where('id',$og['proid'])->find();
						$fuwuendtime = $order['fuwuendtime'] > time() ? $order['fuwuendtime'] + $product['fuwuday']*86400 : time() + $product['fuwuday']*86400;
						Db::name('shop_order')->where('id',$order['id'])->update(['fuwuendtime'=>$fuwuendtime]);
					}
					//自动发货
					if($order['freight_type']==3){
						\app\common\Common::autofh('shop',$order);
					}

					\app\common\Wifiprint::print(aid,'shop',$order['id']);
					//公众号通知 订单支付成功
					$tmplcontent = [];
					$tmplcontent['first'] = '有新订单选择货到付款下单成功';
					$tmplcontent['remark'] = '点击进入查看~';
					$tmplcontent['keyword1'] = $this->member['nickname']; //用户名
					$tmplcontent['keyword2'] = $order['ordernum'];//订单号
					$tmplcontent['keyword3'] = $order['totalprice'].'元';//订单金额
					$tmplcontent['keyword4'] = $order['title'];//商品信息
					\app\common\Wechat::sendhttmpl(aid,$order['bid'],'tmpl_orderpay',$tmplcontent,m_url('admin/order/shoporder'),$order['mdid']);
					$tmplcontent['first'] = '恭喜您的订单已下单成功，支付方式为货到付款';
					$rs = \app\common\Wechat::sendtmpl(aid,mid,'tmpl_orderpay',$tmplcontent,m_url('pages/my/usercenter'));
				}
				//短信通知
				//if($this->member['tel']){
				//	$tel = $this->member['tel'];
				//}else{
				//	$tel = $order['tel'];
				//}
				//$rs = \app\common\Sms::send(aid,$tel,'tmpl_orderpay');

				return $this->json(['status'=>2,'msg'=>'提交成功']);
			}elseif($post['typeid']==10){ //支付宝支付
				$rs = \app\common\Alipay::build_h5(aid,mid,'购买商品',$ordernum,$totalprice,'shop_order2','',m_url('pages/order/orderlist'));
				return $this->json($rs);
			}else{ //微信支付
				if(platform == 'tt' || platform == 'h5'){
					$rs = \app\common\Wxpay::build_h5(aid,mid,'购买商品',$ordernum,$totalprice,'shop_order2');
				}else{
					$rs = \app\common\Wxpay::build(aid,mid,'购买商品',$ordernum,$totalprice,'shop_order2');
				}
				return $this->json($rs);
			}
		}
		$userinfo = [];
		$userinfo['money'] = $this->member['money'];
		if($this->member['paypwd']==''){
			$userinfo['haspwd'] = 0;
		}else{
			$userinfo['haspwd'] = 1;
		}
		$set = Db::name('admin_set')->where('aid',aid)->find();
		$shopset = Db::name('shop_sysset')->where('aid',aid)->find(); 
		//订阅消息
		$wx_tmplset = Db::name('wx_tmplset')->where('aid',aid)->find();
		$tmplids = [];
		if($wx_tmplset['tmpl_orderfahuo']){
			$tmplids[] = $wx_tmplset['tmpl_orderfahuo'];
		}
		$orderids = Db::name('shop_order')->where('ordernum','like',$ordernum.'%')->where('aid',aid)->where('mid',mid)->column('id');
		
		$storeinfo = [];
		if($orderlist[0]['freight_type'] == 1){
			$storeinfo = Db::name('mendian')->where('id',$orderlist[0]['mdid'])->field('name,address,longitude,latitude')->find();
		}

		$rdata = [];
		$rdata['orderinfo'] = $orderlist[0];
		$rdata['wxpayst'] = $set['wxpay'];
		$rdata['alipay'] = $set['alipay'];
		$rdata['moneypay'] = $set['moneypay'];
		$rdata['userinfo'] = $userinfo;
		$rdata['cancod'] = $shopset['cancod'];//货到付款
		$rdata['codtxt'] = $shopset['codtxt'];
		$rdata['totalprice'] = $totalprice;
		$rdata['ordernum'] = $ordernum;
		$rdata['storeinfo'] = $storeinfo;
		$rdata['give_coupon_list'] = \app\common\Coupon::getpaygive(aid,mid,'shop',$totalprice,$orderids);
		$rdata['tmplids'] = $tmplids;
		return $this->json($rdata);
	}
	//积分商城订单支付
	public function scoreshoppay(){
		$orderid = input('param.orderid/d');
		$order = Db::name('scoreshop_order')->where('id',$orderid)->where('aid',aid)->where('mid',mid)->find();
		if(!$order){
			return $this->json(['status'=>0,'msg'=>'该订单不存在']);
		}
		if($order['status'] > 0){
			if($order['status']==4){
				return $this->json(['status'=>0,'msg'=>'该订单已关闭']);
			}else{
				return $this->json(['status'=>0,'msg'=>'该订单已支付']);
			}
		}
		//判断配送时间选择是否符合要求
		if($order['freightid'] && $order['freight_time']){
			$freight = Db::name('freight')->where('id',$order['freightid'])->find();
			if($freight){
				//$freighttime = strtotime(explode('~',$order['freight_time'])[0]);
				$freight_times = explode('~',$order['freight_time']);
				if($freight_times[1]){
					$freighttime = strtotime(explode(' ',$freight_times[0])[0] . ' '.$freight_times[1]);
				}else{
					$freighttime = strtotime($freight_times[0]);
				}
				if(time() + $freight['psprehour']*3600 > $freighttime){
					return $this->json(['status'=>0,'msg'=>($freight['pstype']==0?'配送':'提货').'时间必须在'.$freight['psprehour'].'小时之后']);
				}
			}
		}
		if(request()->isPost()){
			$post = input('post.');
			if($this->member['paypwd'] && $post['typeid']==2){ //余额支付 验证支付密码
				if($post['paypwd'] != $this->member['paypwd']){
					return $this->json(['status'=>0,'msg'=>'支付密码输入错误']);
				}
			}
			if($this->member['score']*1 < $order['totalscore']*1){
				return $this->json(['status'=>0,'msg'=>t('积分').'不足']);
			}
			if($order['totalprice'] <=0){
				//扣积分
				\app\common\Member::addscore(aid,mid,-$order['totalscore'],t('积分').'兑换商品');
				Db::name('scoreshop_order')->where('id',$order['id'])->update(['status'=>1,'paytime'=>time(),'paytype'=>'无须付款']);
				//自动发货
				if($order['freight_type']==3){
					\app\common\Common::autofh('scoreshop',$order);
				}
				//支付后送券
				$couponlist = \app\common\Coupon::getpaygive(aid,mid,'scoreshop',$order['totalprice']);
				if($couponlist){
					foreach($couponlist as $coupon){
						\app\common\Coupon::send(aid,mid,$coupon['id']);
					}
				}
				\app\common\Wifiprint::print(aid,'scoreshop',$order['id']);
				//公众号通知 订单支付成功
				$tmplcontent = [];
				$tmplcontent['first'] = '有新'.t('积分').'商城订单支付成功';
				$tmplcontent['remark'] = '点击进入查看~';
				$tmplcontent['keyword1'] = $this->member['nickname']; //用户名
				$tmplcontent['keyword2'] = $order['ordernum'];//订单号
				$tmplcontent['keyword3'] = $order['totalscore'].t('积分').($order['totalprice']>0?' + '.$order['totalprice'].'元':'');//订单金额
				$tmplcontent['keyword4'] = $order['title'];//商品信息
				\app\common\Wechat::sendhttmpl(aid,0,'tmpl_orderpay',$tmplcontent,m_url('admin/order/scoreshoporder'),$order['mdid']);
				$tmplcontent['first'] = '恭喜您的订单已支付成功';
				$rs = \app\common\Wechat::sendtmpl(aid,mid,'tmpl_orderpay',$tmplcontent,m_url('activity/scoreshop/orderlist'));
				//短信通知
				if($this->member['tel']){
					$tel = $this->member['tel'];
				}else{
					$tel = $order['tel'];
				}
				$rs = \app\common\Sms::send(aid,$tel,'tmpl_orderpay');
				return $this->json(['status'=>2,'msg'=>'兑换成功']);
			}
			if($order['totalprice'] > 0){
				if($post['typeid']==2){ //余额支付
					if($this->member['money'] < $order['totalprice']){
						return $this->json(['status'=>0,'msg'=>t('余额').'不足,请充值']);die;	
					}
					//扣积分
					\app\common\Member::addscore(aid,mid,-$order['totalscore'],t('积分').'兑换商品');
					//减去会员的余额
					\app\common\Member::addmoney(aid,mid,-$order['totalprice'],t('积分').'商城商品,订单号: '.$order['ordernum']);

					Db::name('scoreshop_order')->where('id',$order['id'])->update(['status'=>1,'paytime'=>time(),'paytype'=>t('余额').'支付']);
					
					//自动发货
					if($order['freight_type']==3){
						\app\common\Common::autofh('scoreshop',$order);
					}

					//支付后送券
					$couponlist = \app\common\Coupon::getpaygive(aid,mid,'scoreshop',$order['totalprice']);
					if($couponlist){
						foreach($couponlist as $coupon){
							\app\common\Coupon::send(aid,mid,$coupon['id']);
						}
					}

					\app\common\Wifiprint::print(aid,'scoreshop',$order['id']);

					//公众号通知 订单支付成功
					$tmplcontent = [];
					$tmplcontent['first'] = '有新'.t('积分').'商城订单支付成功';
					$tmplcontent['remark'] = '点击进入查看~';
					$tmplcontent['keyword1'] = $this->member['nickname']; //用户名
					$tmplcontent['keyword2'] = $order['ordernum'];//订单号
					$tmplcontent['keyword3'] = $order['totalscore'].t('积分').($order['totalprice']>0?' + '.$order['totalprice'].'元':'');//订单金额
					$tmplcontent['keyword4'] = $order['title'];//商品信息
					\app\common\Wechat::sendhttmpl(aid,0,'tmpl_orderpay',$tmplcontent,m_url('admin/order/scoreshoporder'),$order['mdid']);
					$tmplcontent['first'] = '恭喜您的订单已支付成功';
					$rs = \app\common\Wechat::sendtmpl(aid,mid,'tmpl_orderpay',$tmplcontent,m_url('activity/scoreshop/orderlist'));
					//短信通知
					if($this->member['tel']){
						$tel = $this->member['tel'];
					}else{
						$tel = $order['tel'];
					}
					$rs = \app\common\Sms::send(aid,$tel,'tmpl_orderpay');

					return $this->json(['status'=>2,'msg'=>'付款成功']);
				}elseif($post['typeid']==10){ //支付宝支付
					$rs = \app\common\Alipay::build_h5(aid,mid,t('积分').'商城商品',$order['ordernum'],$order['totalprice'],'scoreshop_order','',PRE_URL.'/m.php?s=/scoreshop/orderlist/aid/'.aid);
					return $this->json($rs);
				}else{ //微信支付
					if(platform == 'tt' || platform == 'h5'){
						$rs = \app\common\Wxpay::build_h5(aid,mid,t('积分').'商城商品',$order['ordernum'],$order['totalprice'],'scoreshop_order');
					}else{
						$rs = \app\common\Wxpay::build(aid,mid,t('积分').'商城商品',$order['ordernum'],$order['totalprice'],'scoreshop_order');
					}
					return $this->json($rs);
				}
			}
		}

		$userinfo = [];
		$userinfo['money'] = $this->member['money'];
		$userinfo['score'] = $this->member['score'];
		if($this->member['paypwd']==''){
			$userinfo['haspwd'] = 0;
		}else{
			$userinfo['haspwd'] = 1;
		}
		$set = Db::name('admin_set')->where('aid',aid)->find();
		//订阅消息
		$wx_tmplset = Db::name('wx_tmplset')->where('aid',aid)->find();
		$tmplids = [];
		if($wx_tmplset['tmpl_orderfahuo']){
			$tmplids[] = $wx_tmplset['tmpl_orderfahuo'];
		}
		
		$storeinfo = [];
		if($order['freight_type'] == 1){
			$storeinfo = Db::name('mendian')->where('id',$order['mdid'])->field('name,address,longitude,latitude')->find();
		}

		$rdata = [];
		$rdata['orderinfo'] = $order;
		$rdata['storeinfo'] = $storeinfo;
		$rdata['wxpayst'] = $set['wxpay'];
		$rdata['alipay'] = $set['alipay'];
		$rdata['moneypay'] = $set['moneypay'];
		$rdata['userinfo'] = $userinfo;
		$rdata['give_coupon_list'] = \app\common\Coupon::getpaygive(aid,mid,'scoreshop',$order['totalprice']);
		$rdata['tmplids'] = $tmplids;
		return $this->json($rdata);
	}
	//拼团订单支付
	function collagepay(){
		$orderid = input('param.orderid/d');
		$order = Db::name('collage_order')->where('id',$orderid)->where('aid',aid)->where('mid',mid)->find();
		if(!$order){
			return $this->json(['status'=>0,'msg'=>'该订单不存在']);
		}
		if($order['status'] > 0){
			if($order['status']==4){
				return $this->json(['status'=>0,'msg'=>'该订单已关闭']);
			}else{
				return $this->json(['status'=>0,'msg'=>'该订单已支付']);
			}
		}
		//判断配送时间选择是否符合要求
		if($order['freightid'] && $order['freight_time']){
			$freight = Db::name('freight')->where('id',$order['freightid'])->find();
			if($freight){
				//$freighttime = strtotime(explode('~',$order['freight_time'])[0]);
				$freight_times = explode('~',$order['freight_time']);
				if($freight_times[1]){
					$freighttime = strtotime(explode(' ',$freight_times[0])[0] . ' '.$freight_times[1]);
				}else{
					$freighttime = strtotime($freight_times[0]);
				}
				if(time() + $freight['psprehour']*3600 > $freighttime){
					return $this->json(['status'=>0,'msg'=>($freight['pstype']==0?'配送':'提货').'时间必须在'.$freight['psprehour'].'小时之后']);
				}
			}
		}
		if(request()->isPost()){
			$post = input('post.');
			if($this->member['paypwd'] && $post['typeid']==2){ //余额支付 验证支付密码
				if($post['paypwd'] != $this->member['paypwd']){
					return $this->json(['status'=>0,'msg'=>'支付密码输入错误']);
				}
			}
			if($order['buytype']!=1){
				$team = Db::name('collage_order_team')->where('aid',aid)->where('id',$order['teamid'])->find();
				if($team['status']==2){
					return $this->json(['status'=>0,'msg'=>'该团已满员']);
				}
				if($team['status']==3){
					return $this->json(['status'=>0,'msg'=>'该团已解散']);
				}
			}

			if($order['totalprice'] <=0){
				Db::name('collage_order')->where('id',$order['id'])->update(['status'=>1,'paytime'=>time(),'paytype'=>'无须付款']);
				if($order['buytype']!=1){
					$tdata = [];
					$tdata['num'] = $team['num'] + 1;
					if($team['num'] + 1 >= $team['teamnum']){
						$tdata['status'] = 2;
						//团长奖励积分
						$orderlist = Db::name('collage_order')->where(['teamid'=>$team['id'],'status'=>1])->select()->toArray();
						$leader = Db::name('member')->where('id',$team['mid'])->find();
						foreach($orderlist as $v){
							if($v['givescore'] > 0){
								\app\common\Member::addscore(aid,$v['mid'],$v['givescore'],'购买产品奖励'.t('积分'));
							}
							//自动发货
							if($v['freight_type']==3){
								\app\common\Common::autofh('collage',$v);
							}
							//公众号通知 拼团成功通知
							$tmplcontent = [];
							$tmplcontent['first'] = '有新拼团订单拼团成功';
							$tmplcontent['remark'] = '点击进入查看~';
							$tmplcontent['keyword1'] = $order['title']; //商品名称
							$tmplcontent['keyword2'] = $leader['nickname'];//团长
							$tmplcontent['keyword3'] = $team['teamnum'];//成团人数
							//\app\common\Wechat::sendhttmpl(aid,'tmpl_collagesuccess',$tmplcontent,PRE_URL.'/am.php?s=order/collageorder/aid/'.aid);
							$tmplcontent['first'] = '恭喜您拼团成功';
							$rs = \app\common\Wechat::sendtmpl(aid,$v['mid'],'tmpl_collagesuccess',$tmplcontent,m_url('activity/collage/orderlist'));
							//订阅消息
							$tmplcontent = [];
							$tmplcontent['thing1'] = mb_substr($order['title'],0,20);
							$tmplcontent['thing10'] = $leader['nickname'];
							$tmplcontent['number12'] = $team['teamnum'];
							\app\common\Wechat::sendwxtmpl(aid,$v['mid'],'tmpl_collagesuccess',$tmplcontent,'pages/my/usercenter');
							//短信通知
							$rs = \app\common\Sms::send(aid,$this->member['tel']?$this->member['tel']:$tel,'tmpl_collagesuccess');
						}
					}else{
						$tdata['status'] = 1;
					}
					Db::name('collage_order_team')->where('aid',aid)->where('id',$order['teamid'])->update($tdata);
				}else{
					//自动发货
					if($order['freight_type']==3){
						\app\common\Common::autofh('collage',$order);
					}
				}
				
				//支付后送券
				$couponlist = \app\common\Coupon::getpaygive(aid,mid,'collage',$order['totalprice']);
				if($couponlist){
					foreach($couponlist as $coupon){
						\app\common\Coupon::send(aid,mid,$coupon['id']);
					}
				}

				\app\common\Wifiprint::print(aid,'collage',$order['id']);

				//公众号通知 订单支付成功
				$tmplcontent = [];
				$tmplcontent['first'] = '有新拼团订单支付成功';
				$tmplcontent['remark'] = '点击进入查看~';
				$tmplcontent['keyword1'] = $this->member['nickname']; //用户名
				$tmplcontent['keyword2'] = $order['ordernum'];//订单号
				$tmplcontent['keyword3'] = $order['totalprice'].'元';//订单金额
				$tmplcontent['keyword4'] = $order['title'];//商品信息
				\app\common\Wechat::sendhttmpl(aid,$order['bid'],'tmpl_orderpay',$tmplcontent,m_url('admin/order/collageorder'),$order['mdid']);
				$tmplcontent['first'] = '恭喜您的订单已支付成功';
				$rs = \app\common\Wechat::sendtmpl(aid,mid,'tmpl_orderpay',$tmplcontent,m_url('activity/collage/orderlist'));
				//短信通知
				if($order['buytype']==1){ //直接购买
					if($this->member['tel']){
						$tel = $this->member['tel'];
					}else{
						$tel = $order['tel'];
					}
					$rs = \app\common\Sms::send(aid,$tel,'tmpl_orderpay');
				}

				return $this->json(['status'=>2,'msg'=>'付款成功']);
			}
			
			if($post['typeid']==2){ //余额支付
				if($this->member['money'] < $order['totalprice']){
					return $this->json(['status'=>0,'msg'=>t('余额').'不足,请充值']);	
				}
				Db::name('collage_order')->where('id',$order['id'])->update(['status'=>1,'paytime'=>time(),'paytype'=>t('余额').'支付']);
				if($order['buytype']!=1){
					$tdata = [];
					$tdata['num'] = $team['num'] + 1;
					if($team['num'] + 1 >= $team['teamnum']){ //拼团成功
						$tdata['status'] = 2;
						//团长奖励积分
						$orderlist = Db::name('collage_order')->where('teamid',$team['id'])->where('status',1)->select()->toArray();
						$leader = Db::name('member')->where('id',$team['mid'])->find();
						foreach($orderlist as $v){
							if($v['givescore'] > 0){
								\app\common\Member::addscore(aid,$v['mid'],$v['givescore'],'购买产品奖励'.t('积分'));
							}
							//自动发货
							if($v['freight_type']==3){
								\app\common\Common::autofh('collage',$v);
							}
							//公众号通知 拼团成功通知
							$tmplcontent = [];
							$tmplcontent['first'] = '有新拼团订单拼团成功';
							$tmplcontent['remark'] = '点击进入查看~';
							$tmplcontent['keyword1'] = $order['title']; //商品名称
							$tmplcontent['keyword2'] = $leader['nickname'];//团长
							$tmplcontent['keyword3'] = $team['teamnum'];//成团人数
							//\app\common\Wechat::sendhttmpl(aid,'tmpl_collagesuccess',$tmplcontent,PRE_URL.'/am.php?s=order/collageorder/aid/'.aid);
							$tmplcontent['first'] = '恭喜您拼团成功';
							$rs = \app\common\Wechat::sendtmpl(aid,$v['mid'],'tmpl_collagesuccess',$tmplcontent,m_url('activity/collage/orderlist'));
							//订阅消息
							$tmplcontent = [];
							$tmplcontent['thing1'] = mb_substr($order['title'],0,20);
							$tmplcontent['thing10'] = $leader['nickname'];
							$tmplcontent['number12'] = $team['teamnum'];
							\app\common\Wechat::sendwxtmpl(aid,$v['mid'],'tmpl_collagesuccess',$tmplcontent,'pages/my/usercenter');
							//短信通知
							if($this->member['tel']){
								$tel = $this->member['tel'];
							}else{
								$tel = $v['tel'];
							}
							$rs = \app\common\Sms::send(aid,$tel,'tmpl_collagesuccess');
						}
					}else{
						$tdata['status'] = 1;
					}
					Db::name('collage_order_team')->where('aid',aid)->where('id',$order['teamid'])->update($tdata);
				}else{
					//自动发货
					if($order['freight_type']==3){
						\app\common\Common::autofh('collage',$order);
					}
				}

				//减去会员的余额
				\app\common\Member::addmoney(aid,mid,-$order['totalprice'],'拼团商品,订单号: '.$order['ordernum']);

				//支付后送券
				$couponlist = \app\common\Coupon::getpaygive(aid,mid,'collage',$order['totalprice']);
				if($couponlist){
					foreach($couponlist as $coupon){
						\app\common\Coupon::send(aid,mid,$coupon['id']);
					}
				}

				\app\common\Wifiprint::print(aid,'collage',$order['id']);

				//公众号通知 订单支付成功
				$tmplcontent = [];
				$tmplcontent['first'] = '有新拼团订单支付成功';
				$tmplcontent['remark'] = '点击进入查看~';
				$tmplcontent['keyword1'] = $this->member['nickname']; //用户名
				$tmplcontent['keyword2'] = $order['ordernum'];//订单号
				$tmplcontent['keyword3'] = $order['totalprice'].'元';//订单金额
				$tmplcontent['keyword4'] = $order['title'];//商品信息
				\app\common\Wechat::sendhttmpl(aid,$order['bid'],'tmpl_orderpay',$tmplcontent,m_url('admin/order/collageorder'),$order['mdid']);
				$tmplcontent['first'] = '恭喜您的订单已支付成功';
				$rs = \app\common\Wechat::sendtmpl(aid,mid,'tmpl_orderpay',$tmplcontent,m_url('activity/collage/orderlist'));
				//短信通知
				if($order['buytype']==1){ //直接购买
					if($this->member['tel']){
						$tel = $this->member['tel'];
					}else{
						$tel = $order['tel'];
					}
					$rs = \app\common\Sms::send(aid,$tel,'tmpl_orderpay');
				}
				return $this->json(['status'=>2,'msg'=>'付款成功']);
			}elseif($post['typeid']==10){ //支付宝支付
				$rs = \app\common\Alipay::build_h5(aid,mid,'拼团商品',$order['ordernum'],$order['totalprice'],'collage_order','',m_url('activity/collage/orderlist'));
				return $this->json($rs);
			}else{ //微信支付
				if(platform == 'tt' || platform == 'h5'){
					$rs = \app\common\Wxpay::build_h5(aid,mid,'拼团商品',$order['ordernum'],$order['totalprice'],'collage_order');
				}else{
					$rs = \app\common\Wxpay::build(aid,mid,'拼团商品',$order['ordernum'],$order['totalprice'],'collage_order');
				}
				return $this->json($rs);
			}
		}

		$userinfo = [];
		$userinfo['money'] = $this->member['money'];
		if($this->member['paypwd']==''){
			$userinfo['haspwd'] = 0;
		}else{
			$userinfo['haspwd'] = 1;
		}
		$set = Db::name('admin_set')->where('aid',aid)->find();
		//订阅消息
		$wx_tmplset = Db::name('wx_tmplset')->where('aid',aid)->find();
		$tmplids = [];
		if($wx_tmplset['tmpl_orderfahuo']){
			$tmplids[] = $wx_tmplset['tmpl_orderfahuo'];
		}
		if($order['buytype']!=1){
			$tmplids[] = $wx_tmplset['tmpl_collagesuccess'];
		}

		$storeinfo = [];
		if($order['freight_type'] == 1){
			$storeinfo = Db::name('mendian')->where('id',$order['mdid'])->field('name,address,longitude,latitude')->find();
		}

		$rdata = [];
		$rdata['orderinfo'] = $order;
		$rdata['storeinfo'] = $storeinfo;
		$rdata['wxpayst'] = $set['wxpay'];
		$rdata['alipay'] = $set['alipay'];
		$rdata['moneypay'] = $set['moneypay'];
		$rdata['userinfo'] = $userinfo;
		$rdata['give_coupon_list'] = \app\common\Coupon::getpaygive(aid,mid,'collage',$order['totalprice']);
		$rdata['tmplids'] = $tmplids;
		return $this->json($rdata);
	}
	//砍价订单支付
	public function kanjiapay(){
		$orderid = input('param.orderid/d');
		$order = Db::name('kanjia_order')->where('id',$orderid)->where('aid',aid)->where('mid',mid)->find();
		if(!$order){
			return $this->json(['status'=>0,'msg'=>'该订单不存在']);
		}
		if($order['status'] > 0){
			if($order['status']==4){
				return $this->json(['status'=>0,'msg'=>'该订单已关闭']);
			}else{
				return $this->json(['status'=>0,'msg'=>'该订单已支付']);
			}
		}
		if(request()->isPost()){
			$post = input('post.');
			if($this->member['paypwd'] && $post['typeid']==2){ //余额支付 验证支付密码
				if($post['paypwd'] != $this->member['paypwd']){
					return $this->json(['status'=>0,'msg'=>'支付密码输入错误']);
				}
			}
			if($order['totalprice'] <=0){
				Db::name('kanjia_order')->where('id',$order['id'])->update(['status'=>1,'paytime'=>time(),'paytype'=>'无须付款']);
				
				//自动发货
				if($order['freight_type']==3){
					\app\common\Common::autofh('kanjia',$order);
				}
				//支付后送券
				$couponlist = \app\common\Coupon::getpaygive(aid,mid,'kanjia',$order['totalprice']);
				if($couponlist){
					foreach($couponlist as $coupon){
						\app\common\Coupon::send(aid,mid,$coupon['id']);
					}
				}

				\app\common\Wifiprint::print(aid,'kanjia',$order['id']);

				//公众号通知 订单支付成功
				$tmplcontent = [];
				$tmplcontent['first'] = '有新砍价订单支付成功';
				$tmplcontent['remark'] = '点击进入查看~';
				$tmplcontent['keyword1'] = $this->member['nickname']; //用户名
				$tmplcontent['keyword2'] = $order['ordernum'];//订单号
				$tmplcontent['keyword3'] = $order['totalprice'].'元';//订单金额
				$tmplcontent['keyword4'] = $order['title'];//商品信息
				\app\common\Wechat::sendhttmpl(aid,'tmpl_orderpay',$tmplcontent,PRE_URL.'/am.php?s=order/kanjiaorder/aid/'.aid,$order['mdid']);
				$tmplcontent['first'] = '恭喜您的订单已支付成功';
				$rs = \app\common\Wechat::sendtmpl(aid,mid,'tmpl_orderpay',$tmplcontent,m_url('activity/kanjia/orderlist'));
				//短信通知
				if($this->member['tel']){
					$tel = $this->member['tel'];
				}else{
					$tel = $order['tel'];
				}
				$rs = \app\common\Sms::send(aid,$tel,'tmpl_orderpay');

				return $this->json(['status'=>2,'msg'=>'付款成功']);
			}
			
			if($post['typeid']==2){ //余额支付
				if($this->member['money'] < $order['totalprice']){
					return $this->json(['status'=>0,'msg'=>t('余额').'不足,请充值']);	
				}
				Db::name('kanjia_order')->where('id',$order['id'])->update(['status'=>1,'paytime'=>time(),'paytype'=>t('余额').'支付']);
				
				//减去会员的余额
				\app\common\Member::addmoney(aid,mid,-$order['totalprice'],'砍价商品,订单号: '.$order['ordernum']);
				
				//自动发货
				if($order['freight_type']==3){
					\app\common\Common::autofh('kanjia',$order);
				}
				//支付后送券
				$couponlist = \app\common\Coupon::getpaygive(aid,mid,'kanjia',$order['totalprice']);
				if($couponlist){
					foreach($couponlist as $coupon){
						\app\common\Coupon::send(aid,mid,$coupon['id']);
					}
				}

				\app\common\Wifiprint::print(aid,'kanjia',$order['id']);

				//公众号通知 订单支付成功
				$tmplcontent = [];
				$tmplcontent['first'] = '有新砍价订单支付成功';
				$tmplcontent['remark'] = '点击进入查看~';
				$tmplcontent['keyword1'] = $this->member['nickname']; //用户名
				$tmplcontent['keyword2'] = $order['ordernum'];//订单号
				$tmplcontent['keyword3'] = $order['totalprice'].'元';//订单金额
				$tmplcontent['keyword4'] = $order['title'];//商品信息
				\app\common\Wechat::sendhttmpl(aid,'tmpl_orderpay',$tmplcontent,PRE_URL.'/am.php?s=order/kanjiaorder/aid/'.aid,$order['mdid']);
				$tmplcontent['first'] = '恭喜您的订单已支付成功';
				$rs = \app\common\Wechat::sendtmpl(aid,mid,'tmpl_orderpay',$tmplcontent,m_url('activity/kanjia/orderlist'));
				//短信通知
				if($this->member['tel']){
					$tel = $this->member['tel'];
				}else{
					$tel = $order['tel'];
				}
				$rs = \app\common\Sms::send(aid,$tel,'tmpl_orderpay');

				return $this->json(['status'=>2,'msg'=>'付款成功']);
			}elseif($post['typeid']==10){ //支付宝支付
				$rs = \app\common\Alipay::build_h5(aid,mid,'砍价商品',$order['ordernum'],$order['totalprice'],'kanjia_order','',m_url('activity/kanjia/orderlist'));
				return $this->json($rs);
			}else{ //微信支付
				if(platform == 'tt' || platform == 'h5'){
					$rs = \app\common\Wxpay::build_h5(aid,mid,'砍价商品',$order['ordernum'],$order['totalprice'],'kanjia_order');
				}else{
					$rs = \app\common\Wxpay::build(aid,mid,'砍价商品',$order['ordernum'],$order['totalprice'],'kanjia_order');
				}
				return $this->json($rs);
			}
		}
		$userinfo = [];
		$userinfo['money'] = $this->member['money'];
		if($this->member['paypwd']==''){
			$userinfo['haspwd'] = 0;
		}else{
			$userinfo['haspwd'] = 1;
		}
		$set = Db::name('admin_set')->where('aid',aid)->find();
		//订阅消息
		$wx_tmplset = Db::name('wx_tmplset')->where('aid',aid)->find();
		$tmplids = [];
		if($wx_tmplset['tmpl_orderfahuo']){
			$tmplids[] = $wx_tmplset['tmpl_orderfahuo'];
		}
		
		$storeinfo = [];
		if($order['freight_type'] == 1){
			$storeinfo = Db::name('mendian')->where('id',$order['mdid'])->field('name,address,longitude,latitude')->find();
		}
		$rdata = [];
		$rdata['orderinfo'] = $order;
		$rdata['storeinfo'] = $storeinfo;
		$rdata['wxpayst'] = $set['wxpay'];
		$rdata['alipay'] = $set['alipay'];
		$rdata['moneypay'] = $set['moneypay'];
		$rdata['userinfo'] = $userinfo;
		$rdata['give_coupon_list'] = \app\common\Coupon::getpaygive(aid,mid,'kanjia',$order['totalprice']);
		$rdata['tmplids'] = $tmplids;
		return $this->json($rdata);
	}
	
	//秒杀订单支付
	function seckillpay(){
		$orderid = input('param.orderid/d');
		$order = Db::name('seckill_order')->where('id',$orderid)->where('aid',aid)->where('mid',mid)->find();
		if(!$order){
			return $this->json(['status'=>0,'msg'=>'该订单不存在']);
		}
		if($order['status'] > 0){
			if($order['status']==4){
				return $this->json(['status'=>0,'msg'=>'该订单已关闭']);
			}else{
				return $this->json(['status'=>0,'msg'=>'该订单已支付']);
			}
		}
		//判断配送时间选择是否符合要求
		if($order['freightid'] && $order['freight_time']){
			$freight = Db::name('freight')->where('id',$order['freightid'])->find();
			if($freight){
				//$freighttime = strtotime(explode('~',$order['freight_time'])[0]);
				$freight_times = explode('~',$order['freight_time']);
				if($freight_times[1]){
					$freighttime = strtotime(explode(' ',$freight_times[0])[0] . ' '.$freight_times[1]);
				}else{
					$freighttime = strtotime($freight_times[0]);
				}
				if(time() + $freight['psprehour']*3600 > $freighttime){
					return $this->json(['status'=>0,'msg'=>($freight['pstype']==0?'配送':'提货').'时间必须在'.$freight['psprehour'].'小时之后']);
				}
			}
		}
		if(request()->isPost()){
			$post = input('post.');
			if($this->member['paypwd'] && $post['typeid']==2){ //余额支付 验证支付密码
				if($post['paypwd'] != $this->member['paypwd']){
					return $this->json(['status'=>0,'msg'=>'支付密码输入错误']);
				}
			}
			if($order['totalprice'] <=0){
				Db::name('seckill_order')->where('id',$order['id'])->update(['status'=>1,'paytime'=>time(),'paytype'=>'无须付款']);
				
				//自动发货
				if($order['freight_type']==3){
					\app\common\Common::autofh('collage',$order);
				}
				
				//支付后送券
				$couponlist = \app\common\Coupon::getpaygive(aid,mid,'seckill',$order['totalprice']);
				if($couponlist){
					foreach($couponlist as $coupon){
						\app\common\Coupon::send(aid,mid,$coupon['id']);
					}
				}
				\app\common\Wifiprint::print(aid,'seckill',$order['id']);

				//公众号通知 订单支付成功
				$tmplcontent = [];
				$tmplcontent['first'] = '有新秒杀订单支付成功';
				$tmplcontent['remark'] = '点击进入查看~';
				$tmplcontent['keyword1'] = $this->member['nickname']; //用户名
				$tmplcontent['keyword2'] = $order['ordernum'];//订单号
				$tmplcontent['keyword3'] = $order['totalprice'].'元';//订单金额
				$tmplcontent['keyword4'] = $order['title'];//商品信息
				\app\common\Wechat::sendhttmpl(aid,$order['bid'],'tmpl_orderpay',$tmplcontent,m_url('admin/order/seckillorder'),$order['mdid']);
				$tmplcontent['first'] = '恭喜您的订单已支付成功';
				$rs = \app\common\Wechat::sendtmpl(aid,mid,'tmpl_orderpay',$tmplcontent,m_url('activity/seckill/orderlist'));
				//短信通知
				if($order['buytype']==1){ //直接购买
					if($this->member['tel']){
						$tel = $this->member['tel'];
					}else{
						$tel = $order['tel'];
					}
					$rs = \app\common\Sms::send(aid,$tel,'tmpl_orderpay');
				}

				return $this->json(['status'=>2,'msg'=>'付款成功']);
			}
			
			if($post['typeid']==2){ //余额支付
				if($this->member['money'] < $order['totalprice']){
					return $this->json(['status'=>0,'msg'=>t('余额').'不足,请充值']);	
				}
				Db::name('seckill_order')->where('id',$order['id'])->update(['status'=>1,'paytime'=>time(),'paytype'=>t('余额').'支付']);
				
				//自动发货
				if($order['freight_type']==3){
					\app\common\Common::autofh('collage',$order);
				}

				//减去会员的余额
				\app\common\Member::addmoney(aid,mid,-$order['totalprice'],'秒杀商品,订单号: '.$order['ordernum']);

				//支付后送券
				$couponlist = \app\common\Coupon::getpaygive(aid,mid,'seckill',$order['totalprice']);
				if($couponlist){
					foreach($couponlist as $coupon){
						\app\common\Coupon::send(aid,mid,$coupon['id']);
					}
				}

				\app\common\Wifiprint::print(aid,'seckill',$order['id']);

				//公众号通知 订单支付成功
				$tmplcontent = [];
				$tmplcontent['first'] = '有新秒杀订单支付成功';
				$tmplcontent['remark'] = '点击进入查看~';
				$tmplcontent['keyword1'] = $this->member['nickname']; //用户名
				$tmplcontent['keyword2'] = $order['ordernum'];//订单号
				$tmplcontent['keyword3'] = $order['totalprice'].'元';//订单金额
				$tmplcontent['keyword4'] = $order['title'];//商品信息
				if($order['bid']==0){
					\app\common\Wechat::sendhttmpl(aid,'tmpl_orderpay',$tmplcontent,PRE_URL.'/am.php?s=order/seckillorder/aid/'.aid,$order['mdid']);
				}
				$tmplcontent['first'] = '恭喜您的订单已支付成功';
				$rs = \app\common\Wechat::sendtmpl(aid,mid,'tmpl_orderpay',$tmplcontent,m_url('activity/seckill/orderlist'));
				//短信通知
				if($order['buytype']==1){ //直接购买
					if($this->member['tel']){
						$tel = $this->member['tel'];
					}else{
						$tel = $order['tel'];
					}
					$rs = \app\common\Sms::send(aid,$tel,'tmpl_orderpay');
				}
				return $this->json(['status'=>2,'msg'=>'付款成功']);
			}elseif($post['typeid']==10){ //支付宝支付
				$rs = \app\common\Alipay::build_h5(aid,mid,'秒杀商品',$order['ordernum'],$order['totalprice'],'seckill_order','',m_url('activity/seckill/orderlist'));
				return $this->json($rs);
			}else{ //微信支付
				if(platform == 'tt' || platform == 'h5'){
					$rs = \app\common\Wxpay::build_h5(aid,mid,'秒杀商品',$order['ordernum'],$order['totalprice'],'seckill_order');
				}else{
					$rs = \app\common\Wxpay::build(aid,mid,'秒杀商品',$order['ordernum'],$order['totalprice'],'seckill_order');
				}
				return $this->json($rs);
			}
		}

		$userinfo = [];
		$userinfo['money'] = $this->member['money'];
		if($this->member['paypwd']==''){
			$userinfo['haspwd'] = 0;
		}else{
			$userinfo['haspwd'] = 1;
		}
		$set = Db::name('admin_set')->where('aid',aid)->find();
		//订阅消息
		$wx_tmplset = Db::name('wx_tmplset')->where('aid',aid)->find();
		$tmplids = [];
		if($wx_tmplset['tmpl_orderfahuo']){
			$tmplids[] = $wx_tmplset['tmpl_orderfahuo'];
		}

		$storeinfo = [];
		if($order['freight_type'] == 1){
			$storeinfo = Db::name('mendian')->where('id',$order['mdid'])->field('name,address,longitude,latitude')->find();
		}

		$rdata = [];
		$rdata['orderinfo'] = $order;
		$rdata['storeinfo'] = $storeinfo;
		$rdata['wxpayst'] = $set['wxpay'];
		$rdata['alipay'] = $set['alipay'];
		$rdata['moneypay'] = $set['moneypay'];
		$rdata['userinfo'] = $userinfo;
		$rdata['give_coupon_list'] = \app\common\Coupon::getpaygive(aid,mid,'seckill',$order['totalprice']);
		$rdata['tmplids'] = $tmplids;
		return $this->json($rdata);
	}
	
	//升级
	public function uppay(){
		$orderid = input('param.orderid/d');
		$order = Db::name('member_levelup_order')->where('id',$orderid)->where('aid',aid)->where('mid',mid)->find();
		if(!$order){
			return $this->json(['status'=>0,'msg'=>'该订单不存在']);
		}
		if($order['status'] > 0){
			return $this->json(['status'=>0,'msg'=>'该订单已支付']);
		}
		if(request()->isPost()){
			$post = input('post.');
			if($this->member['paypwd'] && $post['typeid']==2){ //余额支付 验证支付密码
				if($post['paypwd'] != $this->member['paypwd']){
					return $this->json(['status'=>0,'msg'=>'支付密码输入错误']);
				}
			}
			$leveldata = Db::name('member_level')->where('aid',aid)->where('id',$order['levelid'])->find();
			$levelid = $leveldata['id'];

			if($order['totalprice'] <=0){
				Db::name('member_levelup_order')->where('id',$order['id'])->update(['status'=>1]);
				if($leveldata['apply_check']){
					Db::name('member')->where('aid',aid)->where('id',mid)->update(['realname'=>$order['realname'],'tel'=>$order['tel'],'weixin'=>$order['weixin'],'aliaccount'=>$order['aliaccount'],'bankcardnum'=>$order['bankcardnum'],'bankcarduser'=>$order['bankcarduser'],'bankname'=>$order['bankname']]);
					$return = ['status'=>2,'msg'=>'付款成功请等待审核'];
				}else{
					Db::name('member')->where('aid',aid)->where('id',mid)->update(['realname'=>$order['realname'],'tel'=>$order['tel'],'weixin'=>$order['weixin'],'aliaccount'=>$order['aliaccount'],'bankcardnum'=>$order['bankcardnum'],'bankcarduser'=>$order['bankcarduser'],'bankname'=>$order['bankname'],'levelid'=>$levelid]);
					Db::name('member_levelup_order')->where('id',$order['id'])->update(['status'=>2]);
					$return = ['status'=>2,'msg'=>'申请成功'];
				}
				return $this->json($return);
			}
			//余额支付
			if($post['typeid']==2){
				if($this->member['money'] < $order['totalprice']){
					return $this->json(['status'=>0,'msg'=>t('余额').'不足,请充值']);	
				}
				Db::name('member_levelup_order')->where('id',$order['id'])->update(['paytype'=>t('余额').'支付','paytime'=>time(),'status'=>1]);
				
				//减去会员的余额
				\app\common\Member::addmoney(aid,mid,-$order['totalprice'],$order['title']);

				if($leveldata['apply_check']){
					Db::name('member')->where('aid',aid)->where('id',mid)->update(['realname'=>$order['realname'],'tel'=>$order['tel'],'weixin'=>$order['weixin'],'aliaccount'=>$order['aliaccount'],'bankcardnum'=>$order['bankcardnum'],'bankcarduser'=>$order['bankcarduser'],'bankname'=>$order['bankname']]);
					$return = ['status'=>2,'msg'=>'付款成功请等待审核'];
				}else{
					if($leveldata['yxqdate'] > 0){
						$levelendtime = strtotime(date('Y-m-d')) + 86400 + 86400 * $leveldata['yxqdate'];
					}else{
						$levelendtime = 0;
					}
					Db::name('member')->where('aid',aid)->where('id',mid)->update(['realname'=>$order['realname'],'tel'=>$order['tel'],'weixin'=>$order['weixin'],'aliaccount'=>$order['aliaccount'],'bankcardnum'=>$order['bankcardnum'],'bankcarduser'=>$order['bankcarduser'],'bankname'=>$order['bankname'],'levelid'=>$levelid,'levelendtime'=>$levelendtime]);
					Db::name('member_levelup_order')->where('id',$order['id'])->update(['status'=>2]);
					
					if($leveldata['apply_payfenxiao'] == 1){ //升级费用参与分销及分红
						applypayfenxiao(aid,$order['id']);
					}
					$return = ['status'=>2,'msg'=>'申请成功'];
				}
				return $this->json($return);
			}elseif($post['typeid']==10){ //支付宝支付
				$rs = \app\common\Alipay::build_h5(aid,mid,'升级['.$leveldata['name'].']',$order['ordernum'],$order['totalprice'],'member_levelup_order','',PRE_URL.'/m.php?s=/my/usercenter/aid/'.aid);
				return $this->json($rs);
			}else{
				if(platform == 'tt' || platform == 'h5'){
					$rs = \app\common\Wxpay::build_h5(aid,mid,'升级['.$leveldata['name'].']',$order['ordernum'],$order['totalprice'],'member_levelup_order');
				}else{
					$rs = \app\common\Wxpay::build(aid,mid,'升级['.$leveldata['name'].']',$order['ordernum'],$order['totalprice'],'member_levelup_order');
				}
				$rs['apply_check'] = $leveldata['apply_check'];
				return $this->json($rs);
			}
		}

		$userinfo = [];
		$userinfo['money'] = $this->member['money'];
		if($this->member['paypwd']==''){
			$userinfo['haspwd'] = 0;
		}else{
			$userinfo['haspwd'] = 1;
		}
		$set = Db::name('admin_set')->where('aid',aid)->find();
		$rdata = [];
		$rdata['orderinfo'] = $order;
		$rdata['wxpayst'] = $set['wxpay'];
		$rdata['alipay'] = $set['alipay'];
		$rdata['moneypay'] = $set['moneypay'];
		$rdata['userinfo'] = $userinfo;
		return $this->json($rdata);
	}
	
	//表单支付
	public function formpay(){
		$orderid = input('param.orderid/d');
		$order = Db::name('form_order')->field('title,money,ordernum,id,formid,mid')->where('id',$orderid)->where('aid',aid)->where('mid',mid)->find();
		if(!$order){
			return $this->json(['status'=>0,'msg'=>'该订单不存在']);
		}
		if($order['paystatus']){
			return $this->json(['status'=>0,'msg'=>'该订单已支付']);
		}
		if(request()->isPost()){
			$post = input('post.');
			if($this->member['paypwd'] && $post['typeid']==2){ //余额支付 验证支付密码
				if($post['paypwd'] != $this->member['paypwd']){
					return $this->json(['status'=>0,'msg'=>'支付密码输入错误']);
				}
			}
			if($order['money'] <=0){
				Db::name('form_order')->where('id',$order['id'])->update(['paystatus'=>1,'paytime'=>time(),'paytype'=>'无须付款']);
				
				$tmplcontent = [];
				$tmplcontent['first'] = '有客户提交表单成功';
				$tmplcontent['remark'] = '请进入电脑端后台进行查看~';
				$tmplcontent['keyword1'] = $order['title'];
				$tmplcontent['keyword2'] = date('Y-m-d H:i');
				\app\common\Wechat::sendhttmpl(aid,'tmpl_formsub',$tmplcontent);

				return $this->json(['status'=>2,'msg'=>'付款成功']);
			}
			
			if($post['typeid']==2){ //余额支付
				if($this->member['money'] < $order['money']){
					return $this->json(['status'=>0,'msg'=>t('余额').'不足,请充值']);	
				}
				Db::name('form_order')->where('id',$order['id'])->update(['paystatus'=>1,'paytime'=>time(),'paytype'=>t('余额').'支付']);

				//减去会员的余额
				\app\common\Member::addmoney(aid,mid,-$order['money'],'表单支付,订单号: '.$order['ordernum']);
				
				$tmplcontent = [];
				$tmplcontent['first'] = '有客户提交表单成功';
				$tmplcontent['remark'] = '请进入电脑端后台进行查看~';
				$tmplcontent['keyword1'] = $order['title'];
				$tmplcontent['keyword2'] = date('Y-m-d H:i');
				\app\common\Wechat::sendhttmpl(aid,'tmpl_formsub',$tmplcontent);

				//分销提成
				$form = Db::name('form')->where('id',$order['formid'])->find();
				if($form && $form['commissionset']!=-1){
					//return $this->json(['status'=>0,'msg'=>'11','data'=>$this->member]);
					$ogdata = [];
					if($this->member['pid']){
						$parent1 = Db::name('member')->where('aid',aid)->where('id',$this->member['pid'])->find();
						if($parent1){
							$agleveldata1 = Db::name('member_level')->where('aid',aid)->where('id',$parent1['levelid'])->find();
							if($agleveldata1['can_agent']!=0){
								$ogdata['parent1'] = $parent1['id'];
							}
						}
					}
					if($parent1['pid']){
						$parent2 = Db::name('member')->where('aid',aid)->where('id',$parent1['pid'])->find();
						if($parent2){
							$agleveldata2 = Db::name('member_level')->where('aid',aid)->where('id',$parent2['levelid'])->find();
							if($agleveldata2['can_agent']>1){
								$ogdata['parent2'] = $parent2['id'];
							}
						}
					}
					if($parent2['pid']){
						$parent3 = Db::name('member')->where('aid',aid)->where('id',$parent2['pid'])->find();
						if($parent3){
							$agleveldata3 = Db::name('member_level')->where('aid',aid)->where('id',$parent3['levelid'])->find();
							if($agleveldata3['can_agent']>2){
								$ogdata['parent3'] = $parent3['id'];
							}
						}
					}
					if($form['commissionset']==1){//按比例
						$commissiondata = json_decode($form['commissiondata1'],true);
						if($commissiondata){
							$ogdata['parent1commission'] = $commissiondata[$agleveldata1['id']]['commission1'] * $order['money'] * 0.01;
							$ogdata['parent2commission'] = $commissiondata[$agleveldata2['id']]['commission2'] * $order['money'] * 0.01;
							$ogdata['parent3commission'] = $commissiondata[$agleveldata3['id']]['commission3'] * $order['money'] * 0.01;
						}
					}elseif($form['commissionset']==2){//按固定金额
						$commissiondata = json_decode($form['commissiondata2'],true);
						if($commissiondata){
							$ogdata['parent1commission'] = $commissiondata[$agleveldata1['id']]['commission1'] * 1;
							$ogdata['parent2commission'] = $commissiondata[$agleveldata2['id']]['commission2'] * 1;
							$ogdata['parent3commission'] = $commissiondata[$agleveldata3['id']]['commission3'] * 1;
						}
					}else{
						$ogdata['parent1commission'] = $agleveldata1['commission1'] * $order['money'] * 0.01;
						$ogdata['parent2commission'] = $agleveldata2['commission2'] * $order['money'] * 0.01;
						$ogdata['parent3commission'] = $agleveldata3['commission3'] * $order['money'] * 0.01;
					}
					
					if($ogdata['parent1'] && $ogdata['parent1commission'] > 0){
						\app\common\Member::addcommission(aid,$ogdata['parent1'],$order['mid'],$ogdata['parent1commission'],'下级购买奖励');
					}
					if($ogdata['parent2'] && $ogdata['parent2commission'] > 0){
						\app\common\Member::addcommission(aid,$ogdata['parent2'],$order['mid'],$ogdata['parent2commission'],'下二级购买奖励');
					}
					if($ogdata['parent3'] && $ogdata['parent3commission'] > 0){
						\app\common\Member::addcommission(aid,$ogdata['parent3'],$order['mid'],$ogdata['parent3commission'],'下三级购买奖励');
					}
					if($ogdata['parent1']){
						\app\common\Member::uplv(aid,$ogdata['parent1']);
					}
				}

				return $this->json(['status'=>2,'msg'=>'付款成功']);
			}elseif($post['typeid']==10){ //支付宝支付
				$rs = \app\common\Alipay::build_h5(aid,mid,'付费',$order['ordernum'],$order['money'],'form_order','',urldecode(input('param.tourl')));
				return $this->json($rs);
			}else{ //微信支付
				if(platform == 'tt' || platform == 'h5'){
					$rs = \app\common\Wxpay::build_h5(aid,mid,'付费',$order['ordernum'],$order['money'],'form_order');
				}else{
					$rs = \app\common\Wxpay::build(aid,mid,'付费',$order['ordernum'],$order['money'],'form_order');
				}
				return $this->json($rs);
			}
		}

		
		$userinfo = [];
		$userinfo['money'] = $this->member['money'];
		if($this->member['paypwd']==''){
			$userinfo['haspwd'] = 0;
		}else{
			$userinfo['haspwd'] = 1;
		}
		$set = Db::name('admin_set')->where('aid',aid)->find();
		$rdata = [];
		$rdata['orderinfo'] = $order;
		$rdata['wxpayst'] = $set['wxpay'];
		$rdata['alipay'] = $set['alipay'];
		$rdata['moneypay'] = $set['moneypay'];
		$rdata['userinfo'] = $userinfo;
		return $this->json($rdata);
	}
	//付费查看页面
	public function pagepay(){
		$orderid = input('param.orderid/d');
		$order = Db::name('designerpage_order')->field('title,price,ordernum,id')->where('id',$orderid)->where('aid',aid)->where('mid',mid)->find();
		if(!$order){
			return $this->json(['status'=>0,'msg'=>'该订单不存在']);
		}
		if($order['status']){
			return $this->json(['status'=>0,'msg'=>'该订单已支付']);
		}

		if(request()->isPost()){
			$post = input('post.');
			if($this->member['paypwd'] && $post['typeid']==2){ //余额支付 验证支付密码
				if($post['paypwd'] != $this->member['paypwd']){
					return $this->json(['status'=>0,'msg'=>'支付密码输入错误']);
				}
			}
			if($order['price'] <=0){
				Db::name('designerpage_order')->where('id',$order['id'])->update(['status'=>1,'paytime'=>time(),'paytype'=>'无须付款']);
				return $this->json(['status'=>2,'msg'=>'付款成功']);
			}
			
			if($post['typeid']==2){ //余额支付
				if($this->member['money'] < $order['price']){
					return $this->json(['status'=>0,'msg'=>t('余额').'不足,请充值']);	
				}
				Db::name('designerpage_order')->where('id',$order['id'])->update(['status'=>1,'paytime'=>time(),'paytype'=>t('余额').'支付']);

				//减去会员的余额
				\app\common\Member::addmoney(aid,mid,-$order['price'],'付费查看,订单号: '.$order['ordernum']);
				return $this->json(['status'=>2,'msg'=>'付款成功']);
			}elseif($post['typeid']==10){ //支付宝支付
				$rs = \app\common\Alipay::build_h5(aid,mid,'付费',$order['ordernum'],$order['price'],'designerpage_order','',urldecode(input('param.tourl')));
				return $this->json($rs);
			}else{ //微信支付
				if(platform == 'tt' || platform == 'h5'){
					$rs = \app\common\Wxpay::build_h5(aid,mid,'付费',$order['ordernum'],$order['price'],'designerpage_order');
				}else{
					$rs = \app\common\Wxpay::build(aid,mid,'付费',$order['ordernum'],$order['price'],'designerpage_order');
				}
				return $this->json($rs);
			}
		}

		
		$userinfo = [];
		$userinfo['money'] = $this->member['money'];
		if($this->member['paypwd']==''){
			$userinfo['haspwd'] = 0;
		}else{
			$userinfo['haspwd'] = 1;
		}
		$set = Db::name('admin_set')->where('aid',aid)->find();
		$rdata = [];
		$rdata['orderinfo'] = $order;
		$rdata['wxpayst'] = $set['wxpay'];
		$rdata['alipay'] = $set['alipay'];
		$rdata['moneypay'] = $set['moneypay'];
		$rdata['userinfo'] = $userinfo;
		return $this->json($rdata);
	}
	//购买优惠券
	public function buycoupon(){
		$id = input('param.id/d');
		$coupon = Db::name('coupon')->where('id',$id)->where('aid',aid)->find();
		if(!$coupon) $this->json(['status'=>0,'msg'=>t('优惠券').'已下架']);
		if(strtotime($coupon['starttime']) > time()) $this->json(['status'=>0,'msg'=>'领取活动还没开始']);
		if(strtotime($coupon['endtime']) < time()) $this->json(['status'=>0,'msg'=>'领取活动已结束']);
		if($coupon['stock']<=0) $this->json(['status'=>0,'msg'=>'已抢光了']);
		$haveget = Db::name('coupon_record')->where('aid',aid)->where('mid',mid)->where('couponid',$id)->count();	
		if($haveget >= $coupon['perlimit']) $this->json(['status'=>0,'msg'=>'您已购买过了']);

		if(request()->isPost()){
			$post = input('post.');
			if($this->member['paypwd'] && $post['typeid']==2){ //余额支付 验证支付密码
				if($post['paypwd'] != $this->member['paypwd']){
					return $this->json(['status'=>0,'msg'=>'支付密码输入错误']);
				}
			}
			if($coupon['score'] > 0){
				if($this->member['score'] < $coupon['score']){
					return $this->json(['status'=>0,'msg'=>t('积分').'不足']);
				}
			}
			if($post['typeid']==2){ //余额支付
				if($this->member['money'] < $coupon['price']){
					return $this->json(['status'=>0,'msg'=>t('余额').'不足,请充值']);	
				}
			}
			//创建订单
			$order = [];
			$order['aid'] = aid;
			$order['mid'] = mid;
			$order['cpid'] = $coupon['id'];
			$order['title'] = $coupon['name'];
			$order['price'] = $coupon['price'];
			$order['ordernum'] = date('ymdHis').aid.rand(1000,9999);
			$order['createtime'] = time();
			$order['status'] = 0;
			$order['id'] = Db::name('coupon_order')->insertGetId($order);

			if($order['price'] <=0){
				Db::name('coupon_order')->where('id',$order['id'])->update(['status'=>1,'paytime'=>time(),'paytype'=>'无须付款']);
				return $this->json(['status'=>2,'msg'=>'付款成功']);
			}
			if($post['typeid']==2){ //余额支付
				Db::name('coupon_order')->where('id',$order['id'])->update(['status'=>1,'paytime'=>time(),'paytype'=>t('余额').'支付']);
				\app\common\Member::addscore($order['aid'],$order['mid'],-$coupon['score'],'兑换'.t('优惠券'));
				\app\common\Member::addmoney($order['aid'],$order['mid'],-$order['price'],'购买'.t('优惠券').':'.$coupon['name']);
				\app\common\Coupon::send($order['aid'],$order['mid'],$order['cpid']);
				return $this->json(['status'=>2,'msg'=>'付款成功']);
			}elseif($post['typeid']==10){ //支付宝支付
				$rs = \app\common\Alipay::build_h5(aid,mid,'购买'.t('优惠券'),$order['ordernum'],$order['price'],'coupon_order','',PRE_URL.'/m.php?s=/coupon/mycoupon/aid/'.aid);
				return $this->json($rs);
			}else{ //微信支付
				if(platform == 'tt' || platform == 'h5'){
					$rs = \app\common\Wxpay::build_h5(aid,mid,'购买'.t('优惠券'),$order['ordernum'],$order['price'],'coupon_order');
				}else{
					$rs = \app\common\Wxpay::build(aid,mid,'购买'.t('优惠券'),$order['ordernum'],$order['price'],'coupon_order');
				}
				return $this->json($rs);
			}
		}

		
		$userinfo = [];
		$userinfo['money'] = $this->member['money'];
		if($this->member['paypwd']==''){
			$userinfo['haspwd'] = 0;
		}else{
			$userinfo['haspwd'] = 1;
		}
		$set = Db::name('admin_set')->where('aid',aid)->find();
		$rdata = [];
		$rdata['coupon'] = $coupon;
		$rdata['wxpayst'] = $set['wxpay'];
		$rdata['alipay'] = $set['alipay'];
		$rdata['moneypay'] = $set['moneypay'];
		$rdata['userinfo'] = $userinfo;
		return $this->json($rdata);
	}
	//买单收款
	public function maidan(){
		if(request()->isPost()){
			$post = input('post.');
			$money = floatval($post['money']);
			if($money <=0){
				return $this->json(['status'=>0,'msg'=>'支付金额必须大于0']);
			}
			if($this->member['paypwd'] && $post['typeid']==2){ //余额支付 验证支付密码
				if($post['paypwd'] != $this->member['paypwd']){
					return $this->json(['status'=>0,'msg'=>'支付密码输入错误']);
				}
			}
			$paymoney = $money;
			//会员折扣
			$disprice = 0;
			$userlevel = Db::name('member_level')->where('aid',aid)->where('id',$this->member['levelid'])->find();
			if($userlevel && $userlevel['discount']>0 && $userlevel['discount']<10){
				$disprice = $paymoney * (1 - $userlevel['discount'] * 0.1);
			}
			$paymoney = $paymoney - $disprice;
			//优惠券
			if($post['couponrid'] > 0){
				$couponrid = $post['couponrid'];
				$couponrecord = Db::name('coupon_record')->where('aid',aid)->where('mid',mid)->where('id',$couponrid)->find();
				if(!$couponrecord){
					return $this->json(['status'=>0,'msg'=>'该'.t('优惠券').'不存在']);
				}elseif($couponrecord['status']!=0){
					return $this->json(['status'=>0,'msg'=>'该'.t('优惠券').'已使用过了']);
				}elseif($couponrecord['starttime'] > time()){
					return $this->json(['status'=>0,'msg'=>'该'.t('优惠券').'尚未开始使用']);	
				}elseif($couponrecord['endtime'] < time()){
					return $this->json(['status'=>0,'msg'=>'该'.t('优惠券').'已过期']);	
				}elseif($couponrecord['minprice'] > $money){
					return $this->json(['status'=>0,'msg'=>'该'.t('优惠券').'不符合条件']);
				}elseif($couponrecord['type']!=1){
					return $this->json(['status'=>0,'msg'=>'该'.t('优惠券').'不符合条件']);	
				}
				$couponmoney = $couponrecord['money'];
				if($couponmoney > $money) $couponmoney = $money;
			}else{
				$couponmoney = 0;
			}
			$paymoney = $paymoney - $couponmoney;
			if($paymoney < 0) $paymoney = 0;
			//积分抵扣
			if($post['usescore']==1){
				$adminset = Db::name('admin_set')->where('aid',aid)->find();
				$score2money = $adminset['score2money'];
				$scoredkmaxpercent = $adminset['scoredkmaxpercent'];
				$scoredk = $this->member['score'] * $score2money;
				if($scoredk > $paymoney) $scoredk = $paymoney;
				if($scoredkmaxpercent > 0 && $scoredkmaxpercent < 100 && $scoredk > 0 && $scoredk > $paymoney * $scoredkmaxpercent * 0.01){
					$scoredk = $paymoney * $scoredkmaxpercent * 0.01;
				}
				$paymoney = $paymoney - $scoredk;
				$paymoney = round($paymoney*100)/100;
				if($paymoney < 0) $paymoney = 0;
				if($scoredk > 0){
					$decscore = $scoredk / $score2money;
				}
			}else{
				$scoredk = 0;
				$decscore = 0;
			}
			if($post['typeid']==2){ //余额支付
				if($this->member['money'] < $paymoney){
					return $this->json(['status'=>0,'msg'=>t('余额').'不足,请充值']);	
				}
			}
			//创建订单
			$order = [];
			$order['ordernum'] = date('ymdHis').aid.rand(1000,9999);
			$order['aid'] = aid;
			$order['mid'] = mid;
			$order['title'] = '买单';
			$order['money'] = $money;
			$order['paymoney'] = $paymoney;
			$order['disprice'] = $disprice;
			$order['scoredk'] = $scoredk;
			$order['decscore'] = $decscore;
			$order['couponrid'] = $couponrid;
			$order['couponmoney'] = $couponmoney; //优惠券抵扣
			$order['createtime'] = time();
			$order['platform'] = platform;
			$order['status'] = 0;
			$order['mdid'] = $post['mdid'];
			$order['id'] = Db::name('maidan_order')->insertGetId($order);

			$order = Db::name('maidan_order')->where('id',$order['id'])->find();

			if($order['paymoney'] <=0){
				Db::name('maidan_order')->where('id',$order['id'])->update(['status'=>1,'paytime'=>time(),'paytype'=>'无须付款']);
				if($order['decscore'] > 0){
					\app\common\Member::addscore($order['aid'],$order['mid'],-$order['decscore'],'买单'.t('积分').'抵扣');
				}
				if($order['couponrid']){
					Db::name('coupon_record')->where('id',$order['couponrid'])->update(['status'=>1,'usetime'=>time()]);
				}
				//公众号通知 订单支付成功
				$tmplcontent = [];
				$tmplcontent['first'] = '有新买单订单支付成功';
				$tmplcontent['remark'] = '点击进入查看~';
				$tmplcontent['keyword1'] = $this->member['nickname']; //用户名
				$tmplcontent['keyword2'] = $order['ordernum'];//订单号
				$tmplcontent['keyword3'] = $order['money'].'元';//订单金额
				$tmplcontent['keyword4'] = $order['title'];//商品信息
				\app\common\Wechat::sendhttmpl(aid,'tmpl_orderpay',$tmplcontent,PRE_URL.'/am.php?s=order/maidanlog/aid/'.aid,$order['mdid']);

				return $this->json(['status'=>2,'msg'=>'付款成功']);
			}
			
			if($post['typeid']==2){ //余额支付
				Db::name('maidan_order')->where('id',$order['id'])->update(['status'=>1,'paytime'=>time(),'paytype'=>t('余额').'支付']);
				if($order['decscore'] > 0){
					\app\common\Member::addscore($order['aid'],$order['mid'],-$order['decscore'],'买单'.t('积分').'抵扣');
				}
				if($order['couponrid']){
					Db::name('coupon_record')->where('id',$order['couponrid'])->update(['status'=>1,'usetime'=>time()]);
				}
				\app\common\Member::addmoney($order['aid'],$order['mid'],-$order['paymoney'],'买单');
				//公众号通知 订单支付成功
				$tmplcontent = [];
				$tmplcontent['first'] = '有新买单订单支付成功';
				$tmplcontent['remark'] = '点击进入查看~';
				$tmplcontent['keyword1'] = $this->member['nickname']; //用户名
				$tmplcontent['keyword2'] = $order['ordernum'];//订单号
				$tmplcontent['keyword3'] = $order['money'].'元';//订单金额
				$tmplcontent['keyword4'] = $order['title'];//商品信息
				\app\common\Wechat::sendhttmpl(aid,'tmpl_orderpay',$tmplcontent,m_url('admin/order/maidanlog'),$order['mdid']);
				return $this->json(['status'=>2,'msg'=>'付款成功']);
			}elseif($post['typeid']==10){ //支付宝支付
				$rs = \app\common\Alipay::build_h5(aid,mid,'买单',$order['ordernum'],$order['paymoney'],'maidan_order','',m_url('pages/maidan/maidanlog'));
				return $this->json($rs);
			}else{ //微信支付
				if(platform == 'h5'){
					$rs = \app\common\Wxpay::build_h5(aid,mid,'买单',$order['ordernum'],$order['paymoney'],'maidan_order');
				}else{
					$rs = \app\common\Wxpay::build(aid,mid,'买单',$order['ordernum'],$order['paymoney'],'maidan_order');
				}
				return $this->json($rs);
			}
		}
		$adminset = Db::name('admin_set')->where('aid',aid)->find();
		$userlevel = Db::name('member_level')->where('aid',aid)->where('id',$this->member['levelid'])->find();
		$userinfo = [];
		$userinfo['discount'] = $userlevel['discount'];
		$userinfo['score'] = $this->member['score'];
		$userinfo['score2money'] = $adminset['score2money'];
		$userinfo['dkmoney'] = $userinfo['score'] * $userinfo['score2money'];
		$userinfo['scoredkmaxpercent'] = $adminset['scoredkmaxpercent'];
		$userinfo['money'] = $this->member['money'];
		if($this->member['paypwd']==''){
			$userinfo['haspwd'] = 0;
		}else{
			$userinfo['haspwd'] = 1;
		}
		$couponList = Db::name('coupon_record')
			->where('aid',aid)->where('mid',mid)->where('type',1)->where('status',0)->where('starttime','<=',time())->where('endtime','>',time())
			->order('id desc')->select()->toArray();
		if(!$couponList) $couponList = [];
		//门店
		$mdlist = Db::name('mendian')->where('aid',aid)->where('status',1)->order('id')->select()->toArray();
		if(!$mdlist) $mdlist = [];
		$set = Db::name('admin_set')->where('aid',aid)->find();
		$rdata = [];
		$rdata['userinfo'] = $userinfo;
		$rdata['couponList'] = $couponList;
		$rdata['wxpayst'] = $set['wxpay'];
		$rdata['alipay'] = $set['alipay'];
		$rdata['moneypay'] = $set['moneypay'];
		$rdata['name'] = $set['name'];
		$rdata['mdlist'] = $mdlist;
		return $this->json($rdata);
	}
}